import { ref, reactive, watchEffect, isReactive } from 'vue'

export default function usePageList(callback, params) {
  let dataSource = reactive({
    total: 0,
    list: []
  })

  function doFetch() {
 
    callback(params).then((res) => {
      if (res.code === 200) {
        if (params.pageNum > 1) {
          dataSource.list = dataSource.list.concat(res.data.list)
        } else {
          dataSource.list = res.data.list
        }
        dataSource.total = res.data.total
      }
    })
  }

  if (isReactive(params)) {
    watchEffect(doFetch)
  }


  return {
    dataSource,
  }
}